<%--
  Created by IntelliJ IDEA.
  User: 石小俊
  Date: 2024/11/16
  Time: 13:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>我的购物车</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {
            $("a[name='modifyCart']").click(function () {
                let id = $(this).attr("data-id");
                // let num = $(this).attr('data-num');
                // 选择器中可以传递两个参数
                // 参数一:匹配规则
                // 参数二:查找范围
                let num = $("input[type='text']", $(this).parent().parent()).val();
                // console.log("id:"+id+",num:"+num)
                location.href = "${pageContext.request.contextPath}/modify.cart?id=" + id + "&num=" + num;
            })
            // 全选
            $("#all").click(function () {
                $("input[type='checkbox']").prop("checked", $(this).prop("checked"))
            });
            // 删除选中项
            $("#removeCarts").click(function(){
                $("#cartForm").submit();
            });
        })
    </script>
</head>
<body>
<h1>我的购物车</h1>
<hr>
<form id="cartForm" action="${pageContext.request.contextPath}/removeByIds.cart">
    <table border="1">
        <tr>
            <th><input type="checkbox" id="all">全选</th>
            <th>序号</th>
            <th>商品名称</th>
            <th>商品数量</th>
            <th>商品总价</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${cart.items}" var="item" varStatus="i">
            <tr>
                <td><input type="checkbox" value="${item.product.id}" name="id"></td>
                <td>${i.index + 1}</td>
                <td>${item.product.name}</td>
                <td><input type="text" value="${item.num}"></td>
                <td>${item.price}</td>
                <td>
                    <a href="javascript:;" name="modifyCart" data-id="${item.product.id}">修改</a>
                    <a href="${pageContext.request.contextPath}/removeById.cart?id=${item.product.id}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</form>
------------------------------------------------<br>
总计:${cart.price}元<br>
<a href="javascript:;" id="removeCarts">删除选中项</a>
<a href="${pageContext.request.contextPath}/clean.cart">清空购物车</a>
<a href="${pageContext.request.contextPath}/findAll.product">返回购物</a>
<a href="${pageContext.request.contextPath}/create.order">结算</a><br>
<span style="color:red;">${cartMsg}</span>
</body>
</html>
